package com.smartfactory.sail.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smartfactory.sail.dto.DeviceUseStatusDto;
import com.smartfactory.sail.entity.Device;
import com.smartfactory.sail.entity.enums.DeviceStatusEnum;
import com.smartfactory.sail.mapper.DeviceMaintenanceMapper;
import com.smartfactory.sail.mapper.DeviceMapper;
import com.smartfactory.sail.service.IDeviceService;
import com.smartfactory.sail.utils.PageUtils;
import com.smartfactory.sail.vo.PageParam;
import com.smartfactory.sail.vo.device.ListDeviceResp;
import org.springframework.stereotype.Service;

import java.text.NumberFormat;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 设备信息 服务实现类
 * </p>
 *
 * @author 王巨壁
 * @since 2019-07-24
 */
@Service
public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> implements IDeviceService {

    private DeviceMapper deviceMapper;

    private DeviceMaintenanceMapper deviceMaintenanceMapper;

    public DeviceServiceImpl(DeviceMapper deviceMapper,DeviceMaintenanceMapper deviceMaintenanceMapper) {
        this.deviceMapper = deviceMapper;
        this.deviceMaintenanceMapper = deviceMaintenanceMapper;
    }

    @Override
    public IPage<ListDeviceResp> getListAndPages(Map<String, Object> queryParam, PageParam pageParam) {
        //分页信息
        Page<ListDeviceResp> page = new Page<>();
        PageUtils.createPage(page, pageParam);
        QueryWrapper<ListDeviceResp> query = new QueryWrapper<>();
        query.allEq(queryParam);
        return deviceMapper.getPage(page, query);
    }

    @Override
    public List<Device> getDeviceTypes() {
        return deviceMapper.getDeviceTypes();
    }

    @Override
    public boolean updateNextMaintenanceDateById(Long id) {
        return deviceMapper.updateNextMaintenanceDateById(id);
    }

    @Override
    public void updateStatus(Long id, DeviceStatusEnum status) {
        deviceMapper.updateStatus(id, status);
    }

    @Override
    public DeviceUseStatusDto getDeviceUseStatus() {
        DeviceUseStatusDto deviceUseStatusDto = new DeviceUseStatusDto();
        //设备数量
        Integer deviceNum = deviceMapper.getDeviceNum();
        deviceUseStatusDto.setDeviceNum(deviceNum);
        //本周维修设备数
        Integer deviceMaintenanceNum = deviceMaintenanceMapper.getDeviceMaintenanceNum();
        deviceUseStatusDto.setMaintenanceDeviceNum(deviceMaintenanceNum);
        //本周维修次数
        Integer maintenanceNum = deviceMaintenanceMapper.getMaintenanceNum();
        deviceUseStatusDto.setMaintenanceNum(maintenanceNum);
        // 创建一个数值格式化对象
        NumberFormat numberFormat = NumberFormat.getInstance();
        // 设置精确到小数点后2位
        numberFormat.setMaximumFractionDigits(2);
        //本周故障率
        String maintentancePer = numberFormat.format((float) deviceMaintenanceNum / (float) deviceNum * 100)+"%";
        deviceUseStatusDto.setMaintentancePer(maintentancePer);

        return deviceUseStatusDto;
    }
}
